﻿<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="XmlQuery"
    targetNamespace="http://rql-rapid.org/RapidXmlSchema.xsd"
    elementFormDefault="qualified"
    xmlns="http://rql-rapid.org/RapidXmlSchema.xsd"
    xmlns:mstns="http://rql-rapid.org/RapidXmlSchema.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
  <xs:annotation>
    <xs:documentation>
      XML Query API. An XML view of RQL
    </xs:documentation>
  </xs:annotation>

  <xs:element name="Query">
    <xs:complexType>
      <xs:sequence>
          <xs:sequence minOccurs="0">
            <xs:element name="From" type="FromType" minOccurs="1" maxOccurs="1" />
            <xs:element name="Where" type="WhereType" minOccurs="0"  />
            <xs:element name="GroupBy" type="GroupByType" minOccurs="0" />
            <xs:element name="OrderByAscending" type="OrderbyAscendingType" minOccurs="0" />
            <xs:element name="OrderbyDescending" type ="OrderbyDescendingType" minOccurs="0" />
            <xs:element name="Select" type="SelectType" minOccurs="0" maxOccurs="1" />            
          </xs:sequence>
          <xs:sequence minOccurs="0" maxOccurs="unbounded">
            <xs:element name="row" type="RowSetType" />            
          </xs:sequence>
        </xs:sequence>                      
      <xs:attribute name="version" type="xs:string" default="1.0" />    
    </xs:complexType>   
  </xs:element>

  <xs:simpleType name="ConditionalType">
    <xs:restriction base="xs:string">
      <xs:enumeration value="Equals" />
      <xs:enumeration value="GreaterThan" />
      <xs:enumeration value="LessThan"  />
      <xs:enumeration value="GreaterThanEqualsTo"  />
      <xs:enumeration value="LessThanEqualsTo" />
      <xs:enumeration value="NotEquals" />
      <xs:enumeration value="Is" />
      <xs:enumeration value="IsNull" />
      <xs:enumeration value="IsNotNull" />
      <xs:enumeration value="StartsWith"  />
      <xs:enumeration value="EndsWith" />
      <xs:enumeration value="Contains" />
      <xs:enumeration value="NotContains" />
      <xs:enumeration value="IN" />
      <xs:enumeration value="NOT_IN" />
      <xs:enumeration value="Between" />
      <xs:enumeration value="NOTSET" />
    </xs:restriction>
  </xs:simpleType>

  <xs:complexType name="FromType">
    <xs:sequence minOccurs="0">
      <xs:element name="Join" type="JoinType" />
      <xs:element name="In" type="InType" />
    </xs:sequence>
    <xs:attribute name="Typeof" use="required" type="xs:string" />
    <xs:attribute name="As" use="required" type="xs:string" />
  </xs:complexType>

  <xs:complexType name="JoinType">
    <xs:attribute name="Property" use="required" type="xs:string" />
    <xs:attribute name="As" use="required" type="xs:string" />
  </xs:complexType>

  <xs:complexType name="InType">
    <xs:attribute name="Property" use="required" type="xs:string" />
    <xs:attribute name="As" use="required" type="xs:string" />
  </xs:complexType>

  <xs:complexType name="WhereType">
    <xs:sequence minOccurs="0" maxOccurs="unbounded">
      <xs:element name="Expression" type="ExpressionType" minOccurs="1" />
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="GroupByType">
    <xs:sequence minOccurs="0" maxOccurs="unbounded">
      <xs:element name="Expression" type="ExpressionType" minOccurs="1" />
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="OrderbyAscendingType">
    <xs:sequence minOccurs="0" maxOccurs="unbounded">
      <xs:element name="Expression" type="ExpressionType" minOccurs="1" />
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="OrderbyDescendingType">
    <xs:sequence minOccurs="0" maxOccurs="unbounded">
      <xs:element name="Expression" type="ExpressionType" minOccurs="1" />
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="SelectType">
    <xs:sequence minOccurs="1" maxOccurs="unbounded">
      <xs:element name="Expression" type="ExpressionType" minOccurs="1"  />
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="RowSetType">
    <xs:sequence minOccurs="0" maxOccurs="unbounded">
      <xs:element name="column" type="ColumnSetType"/>     
    </xs:sequence>
    <xs:attribute name="rowid" use="required" type="xs:integer" />
  </xs:complexType>

  <xs:complexType name="ColumnSetType">
    <xs:attribute name="Name" use="required" />
    <xs:attribute name="Value" use="optional" />
  </xs:complexType>

  <xs:complexType name="ExpressionType">
    <xs:attribute name="Name" use="required" />    
    <xs:attribute name="Condition" use="optional" type="ConditionalType" default="NOTSET" />
    <xs:attribute name="Value" use="optional" />
  </xs:complexType>
</xs:schema>
